package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("setmeal")
public class    SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private DishService dishService;

    /**
     * 添加套餐
     * SetmealDto = Setmeal + List<SetmealDish>
     *
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> saveWithDish(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithDish(setmealDto);
        return R.success("添加成功");
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("page")
    public R<Page<SetmealDto>> page(Integer page, Integer pageSize, String name) {

        Page<SetmealDto> setmealDtoPage = setmealService.pageSetmealDto(page, pageSize, name);
        return R.success(setmealDtoPage);
    }

    /**
     * 删除套餐,删除套餐菜品关联数据
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    public R<String> deleteByIds(Long[] ids) {
        setmealService.deleteByIds(ids);
        return R.success("删除成功");
    }

    /**
     * 启售,停售状态
     *
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("status/{status}")
    public R<String> updateStatus(@PathVariable Integer status, Long[] ids) {
        //批量修改状态
        //update setmeal status=? where id in (ids)
        setmealService.lambdaUpdate()
                //修改值
                .set(Setmeal::getStatus, status)
                //条件部分
                .in(Setmeal::getId, ids)
                //更新操作
                .update();

        return R.success("修改成功");

    }

    /**
     * 根据id查询套餐详细信息(包含多个菜品)
     * 确定返回一个对象
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R<SetmealDto> getById(@PathVariable Long id) {
        SetmealDto setmealDto = setmealService.getByIdWithDish(id);
        return R.success(setmealDto);
    }

    /**
     * 修改套餐
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        //保存菜品,菜品口味数据
        setmealService.updateWithDish(setmealDto);
        return R.success("修改成功");
    }

    /**
     * 根据分类查询套餐分类
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("list")
    @Cacheable(cacheNames = "setmeallist",key = "#categoryId",unless = "#result.data.size()==0")
    public R<List<Setmeal>> list(Long categoryId, Integer status){

        //select * from setmeal where category_id=? and status=1
        List<Setmeal> list = setmealService.lambdaQuery()
                .eq(categoryId!=null,Setmeal::getCategoryId, categoryId)
                .eq(Setmeal::getStatus, 1)
                .list();

        return R.success(list);
    }


    /**
     * 根据id查询套餐对应的菜品信息
     * @param setmealId
     * @return
     */
    @GetMapping("dish/{setmealId}")
    public R<List<DishDto>> getDishBySetmealId(@PathVariable Long setmealId){

        List<DishDto> dishList=dishService.getDishBySetmealId(setmealId);

        return R.success(dishList);

    }

    
}
